package com.ruoyi.sale.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sale.domain.ErpGoodss;
import com.ruoyi.sale.domain.ErpOrder;
import com.ruoyi.sale.domain.ErpOrderReturnItem;
import com.ruoyi.sale.service.IErpOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sale.mapper.ErpOrderReturnMappers;
import com.ruoyi.sale.domain.ErpOrderReturns;
import com.ruoyi.sale.service.IErpOrderReturnService;

/**
 * 订单退货Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-15
 */
@Service
public class ErpOrderReturnServiceImpls implements IErpOrderReturnService
{
    @Autowired
    private ErpOrderReturnMappers erpOrderReturnMappers;

    @Autowired
    private IErpOrderService erpOrderService;



    @Override
    public void syncReturnStatistics(Integer returnId) {
        // 调用Mapper同步数据到统计表
        erpOrderReturnMappers.syncReturnStatistics(returnId);
    }

    @Override
    public void addReturnItem(Integer returnId) {
        ErpOrderReturns returnOrder = erpOrderReturnMappers.selectErpOrderReturnById(returnId);

        // 2. 构造退货项数据
        ErpOrderReturnItem returnItem = new ErpOrderReturnItem();
        returnItem.setOrderReturnId(Long.valueOf(returnId));
        returnItem.setGoodsId(1L); // 示例商品ID，实际应从订单中获取
        returnItem.setProductName("示例商品");
        returnItem.setReturnNum(1); // 示例退货数量
        returnItem.setProductPrice(BigDecimal.valueOf(100.00)); // 示例单价

        // 3. 插入数据
        erpOrderReturnMappers.insertReturnItem(returnItem);
    }

    /**
     * 查询订单退货
     *
     * @param id 订单退货主键
     * @return 订单退货
     */
    @Override
    public ErpOrderReturns selectErpOrderReturnById(Integer id)
    {
        return erpOrderReturnMappers.selectErpOrderReturnById(id);
    }


    /**
     * 查询订单退货列表
     *
     * @param erpOrderReturns 订单退货
     * @return 订单退货
     */
    @Override
    public List<ErpOrderReturns> selectErpOrderReturnList(ErpOrderReturns erpOrderReturns)
    {
        return erpOrderReturnMappers.selectErpOrderReturnList(erpOrderReturns);
    }

    /**
     * 新增订单退货
     *
     * @param erpOrderReturns 订单退货
     * @return 结果
     */
    @Override
    public int insertErpOrderReturn(ErpOrderReturns erpOrderReturns)
    {
        ErpOrder order = erpOrderService.selectErpOrderById(erpOrderReturns.getOrderId().intValue());
        if (order == null) {
            throw new RuntimeException("关联订单不存在");
        }

        // 自动设置仓库和部门信息
        if (erpOrderReturns.getWarehouseId() == null) {
            erpOrderReturns.setWarehouseId(order.getWarehouseId());
            erpOrderReturns.setWarehouseName(order.getWarehouseName());
        }

        if (erpOrderReturns.getDeptId() == null) {
            erpOrderReturns.setDeptId(order.getDeptId());
            erpOrderReturns.setDeptName(order.getDeptName());
        }

        String returnSn = generateReturnSn();
        erpOrderReturns.setSn(returnSn);
        erpOrderReturns.setStatus("未提交"); // 设置默认状态
        erpOrderReturns.setCreateTime(DateUtils.getNowDate());
        return erpOrderReturnMappers.insertErpOrderReturn(erpOrderReturns);
    }
    private String generateReturnSn() {
        // 获取当前日期
        String dateStr = DateUtils.dateTimeNow("yyyyMMdd");

        // 查询当天最大的序列号
        Integer maxSeq = erpOrderReturnMappers.selectMaxSeqByDate(dateStr);
        if (maxSeq == null) {
            maxSeq = 0;
        }

        // 生成新序列号
        int newSeq = maxSeq + 1;

        // 格式化序列号为4位数字
        String seqStr = String.format("%04d", newSeq);

        return "TH-" + dateStr + "-" + seqStr;
    }
    /**
     * 修改订单退货
     *
     * @param erpOrderReturns 订单退货
     * @return 结果
     */
    @Override
    public int updateErpOrderReturn(ErpOrderReturns erpOrderReturns)
    {
        erpOrderReturns.setUpdateTime(DateUtils.getNowDate());
        return erpOrderReturnMappers.updateErpOrderReturn(erpOrderReturns);
    }

    /**
     * 批量删除订单退货
     *
     * @param ids 需要删除的订单退货主键
     * @return 结果
     */
    @Override
    public int deleteErpOrderReturnByIds(Integer[] ids)
    {
        return erpOrderReturnMappers.deleteErpOrderReturnByIds(ids);
    }

    /**
     * 删除订单退货信息
     *
     * @param id 订单退货主键
     * @return 结果
     */
    @Override
    public int deleteErpOrderReturnById(Integer id)
    {
        return erpOrderReturnMappers.deleteErpOrderReturnById(id);
    }

    @Override
    public List<ErpGoodss> selectByOrderId(Long orderId) {
        return erpOrderReturnMappers.selectByOrderId(orderId);
    }
}
